<template>
  <div id="userlogin">
    <div id="bigbox">
      <div id="imgbox">
        <img src="../assets/img/logo.png" />
      </div>

      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="el-icon-user-solid"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-s-cooperation"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :offset="15">
            <el-button type="primary" @click="logout()">登陆</el-button>
            <el-button type="info" @click="reseForm">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456"
      },
      loginFormRules: {
        username: [{ required: true, message: "请输入名称", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    logout() {
      this.$refs.loginFormRef.validate(async valid => {
        if (valid === true) {
          const { data: res } = await this.$http.post("/login", this.loginForm);
          if (res.meta.status !== 200) {
            return this.$message.error("用户名或密码不存在");
          }
          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/home");
        }
      });
    },
    reseForm() {
      this.$refs.loginFormRef.resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
#userlogin {
  background-color: #2b4b6b;
  height: 100%;
  overflow: hidden;
  #bigbox {
    height: 304px;
    width: 450px;
    background-color: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    #imgbox {
      border: 1px solid #eee;
      background-color: #fff;
      height: 148px;
      width: 148px;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      box-sizing: border-box;
      box-shadow: 0 0 10px #eee;
      img {
        border-radius: 50%;
        height: 100%;
        width: 100%;
        background-color: #eee;
      }
    }
    .el-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 25px;
      box-sizing: border-box;
    }
  }
}
</style>
